//
//  YPhotoCollectionViewCell.m
//  AXLPhotoBower
//
//  Created by gome on 2018/5/4.
//  Copyright © 2018年 gome. All rights reserved.
//

#import "JCPhotoCollectionViewCell.h"
#import "JCPhotoGlobalVar.h"


@interface JCPhotoCollectionViewCell()

@property (strong,nonatomic) JCPhotoGlobalVar * globalVar;

@end

@implementation JCPhotoCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    if (self) {
        
        _globalVar = [JCPhotoGlobalVar shareGlobalVar];
        [self imageView];
        [self button];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationWhenSelectPicture) name:NotificationWhenSelectPictureName object:nil];
        
    }
    
    return self;
}

- (UIButton *)button {
    if (!_button) {
        _button = [[UIButton alloc] init];
        [self addSubview:_button];
        
        [_button setFrame:CGRectMake(self.frame.size.width - 40, 0, 40, 40)];
        [_button addTarget:self action:@selector(selectOrDeselect) forControlEvents:UIControlEventTouchUpInside];
        [_button setImage:[UIImage imageNamed:@"JCPhoto.bundle/y_icon_image_no"] forState:UIControlStateNormal];
        
    }
    
    return _button;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        [self addSubview:_imageView];
        
        [_imageView setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.width)];
    }
    
    return _imageView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)selectOrDeselect {
    [self updateUI];
}

- (void)updateUI {    
    if (_isPicked == NO) {//当前未被选择
        if (_globalVar.maxNum == _globalVar.currentNum) {
            NSString * title = [NSString stringWithFormat:@"您最多只能选择%d张图片",_globalVar.maxNum];
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:title message:nil delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
            [alert show];
            return;
        }
        
        if (_globalVar.animation == nil) {
            _globalVar.animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
            _globalVar.animation.duration = 0.15;
            _globalVar.animation.autoreverses = false;
            _globalVar.animation.values = @[@1.0,@1.2,@1.0];
            _globalVar.animation.fillMode = kCAFillModeBackwards;
        }

        _isPicked = YES;

        if (_selectBlock) {
            self.selectBlock(_isPicked, self);
        }
    }
    else if (_isPicked == YES) {
        _isPicked = NO;
        if (_selectBlock) {
            self.selectBlock(_isPicked, self);
        }
    }
}

- (void)notificationWhenSelectPicture {

    if (_isPicked == YES) {//当前未被选择
        
        [_button setImage:[UIImage imageNamed:@"JCPhoto.bundle/y_icon_image_yes"] forState:UIControlStateNormal];
        [_button.layer removeAnimationForKey:@"transform.scale"];
        [_button.layer addAnimation:_globalVar.animation forKey:@"transform.scale"];
        
        
        if (self.indexPath.row != NSIndexPathMax.row && [_globalVar.indexPaths containsObject:self.indexPath]) {
            NSInteger index = [_globalVar.indexPaths indexOfObject:self.indexPath];
            self.index = index;
        }
        
        if (self.index != NSIntegerMax) {
            [_button setTitle:[NSString stringWithFormat:@"%ld", (self.index + 1)] forState:UIControlStateNormal];
        }
        
        [_button setTitleEdgeInsets:UIEdgeInsetsMake(0, -(_button.imageView.frame.size.width / 2.0 + _button.titleLabel.frame.size.width / 2.0), 0, _button.imageView.frame.size.width / 2.0 + _button.titleLabel.frame.size.width / 2.0)];
    }

}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NotificationWhenSelectPictureName object:nil];
}
@end
